import json
import time
from selenium import webdriver
from bs4 import BeautifulSoup

def get_login():
    driver=webdriver.PhantomJS(executable_path='E:\\Program Files\\phantomjs-2.1.1-windows\\phantomjs.exe')
    driver.maximize_window()

    driver.get('https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2Fmsg%2Fchat%3Fuid%3D2977539467%26nick%3Dkonkon_si%26verified_type%3D-1%26luicode%3D10000011%26lfid%3D1005052977539467%26featurecode%3D20000320')
    time.sleep(2)

    try:
        driver.find_element_by_id('loginWrapper')
        wapper=True
    except Exception as e:
        wapper=False
        print(e)

    try:
        if wapper:
            driver.find_element_by_id('loginName').clear()
            driver.find_element_by_id('loginName').send_keys('junzhenggg@163.com')
            driver.find_element_by_id('loginPassword').clear()
            driver.find_element_by_id('loginPassword').send_keys('abc@123')
            driver.find_element_by_id('loginAction').click()

            driver.find_elements_by_css_selector('div.fl.drop-title')
            cook=driver.get_cookies()
            ck_file=open('../file/ck.txt','w')
            for c in cook:
                ck_file.write(c['name']+'='+c['value']+';')
                print(c['name']+":"+c['value'])

            home_page=driver.page_source
            soup=BeautifulSoup(home_page,'lmxl')
            print(soup.prettify())
    except Exception as e:
        print(e)

if __name__=='__main__':
    get_login()